package cn.ds.assets.module.assets.controller.admin.metadataJobCollect;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataJobCollect.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataJobCollect.BaseMetadataJobCollectDO;
import cn.ds.assets.module.assets.convert.metadataJobCollect.BaseMetadataJobCollectConvert;
import cn.ds.assets.module.assets.service.metadataJobCollect.BaseMetadataJobCollectService;

//@Tag(name = "管理后台 - 元数据管理-元数据采集作业")
//@RestController
//@RequestMapping("/assets/base-metadata-job-collect")
@Validated
public class BaseMetadataJobCollectController {

    @Resource
    private BaseMetadataJobCollectService baseMetadataJobCollectService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-元数据采集作业")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:create')")
    public CommonResult<Long> createBaseMetadataJobCollect(@Valid @RequestBody BaseMetadataJobCollectCreateReqVO createReqVO) {
        return success(baseMetadataJobCollectService.createBaseMetadataJobCollect(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-元数据采集作业")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:update')")
    public CommonResult<Boolean> updateBaseMetadataJobCollect(@Valid @RequestBody BaseMetadataJobCollectUpdateReqVO updateReqVO) {
        baseMetadataJobCollectService.updateBaseMetadataJobCollect(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-元数据采集作业")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:delete')")
    public CommonResult<Boolean> deleteBaseMetadataJobCollect(@RequestParam("id") Long id) {
        baseMetadataJobCollectService.deleteBaseMetadataJobCollect(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-元数据采集作业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:query')")
    public CommonResult<BaseMetadataJobCollectRespVO> getBaseMetadataJobCollect(@RequestParam("id") Long id) {
        BaseMetadataJobCollectDO baseMetadataJobCollect = baseMetadataJobCollectService.getBaseMetadataJobCollect(id);
        return success(BaseMetadataJobCollectConvert.INSTANCE.convert(baseMetadataJobCollect));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-元数据采集作业列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:query')")
    public CommonResult<List<BaseMetadataJobCollectRespVO>> getBaseMetadataJobCollectList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataJobCollectDO> list = baseMetadataJobCollectService.getBaseMetadataJobCollectList(ids);
        return success(BaseMetadataJobCollectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-元数据采集作业分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:query')")
    public CommonResult<PageResult<BaseMetadataJobCollectRespVO>> getBaseMetadataJobCollectPage(@Valid BaseMetadataJobCollectPageReqVO pageVO) {
        PageResult<BaseMetadataJobCollectDO> pageResult = baseMetadataJobCollectService.getBaseMetadataJobCollectPage(pageVO);
        return success(BaseMetadataJobCollectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-元数据采集作业 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-collect:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataJobCollectExcel(@Valid BaseMetadataJobCollectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataJobCollectDO> list = baseMetadataJobCollectService.getBaseMetadataJobCollectList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataJobCollectExcelVO> datas = BaseMetadataJobCollectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-元数据采集作业.xls", "数据", BaseMetadataJobCollectExcelVO.class, datas);
    }

}
